<?php

namespace app\api\controller;

use app\common\business\User as UserBus;

class User extends AuthBase
{
    public function index()
    {
        $user = (new UserBus())->getNormalUserById($this->userId);

        $result = [
            'id' => $user['id'],
            'username' => $user['username'],
            'sex' => $user['sex'],

        ];
        return show(config('status.success'),'OK', $user);
    }

    public function update()
    {
        $username = input('param.username','','trim');
        $sex = input('param.sex',0, 'intval');

        $data = [
            'username' => $username,
            'sex' => $sex,
        ];
        $validate = (new \app\api\validate\User())->scene('update_user');
        if(!$validate->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        $user = (new UserBus())->update($this->userId, $data);
        if (!$user) {
            return show(config('status.error'), '更新失败');
        }

        //修改redis里的数据
        $data = array_merge($data, ['token' => $this->accessToken]);
        $res = (new UserBus())->updateRedis($this->userId, $data);
        if (!$res) {
            return show(config('status.error'), 'redis更新失败');
        }
        return show(0, "OK");
    }
}